Start_Program:

 XOR A
 LD (Lobby_Host_Or_Join), A

;Of course, the first menu is the main menu

 SET Main_Menu_Or_New_Game_Menu, (IY + Flag)


;Display the Splash Screen
 call Draw_Splash_Screen_Background
 call fastCopyWithoutClear

 B_CALL _getKey
 SET textWrite, (IY + sGrFlags)
 CALL Load_Game

 RES fracDrawLFont, (IY + fontFlags)
 call Draw_Splash_Screen_Background


;The Menu Background is a white oval for all the text, overlayed
;below the Splash Screen's "S.A.D"

Draw_Menu_Background:
 Call Draw_Main_Menu_Background
 BIT Main_Menu_Or_New_Game_Menu, (IY + Flag)
 JR Z, Draw_New_Game_Menu_Text


Draw_Main_Menu_Text:
 DrawSmallText(36,27, Main_Menu_Text)
 DrawSmallText(36,34, Main_Menu_Text + 9)
 DrawSmallText(36,41, Main_Menu_Text + 19)
 DrawSmallText(36,48, Main_Menu_Text + 28)
 DrawSmallText(36,55, Main_Menu_Text + 36)

 LD A, 1
 LD (Sprite_Width), A

 JR Main_Menu_Get_Key



Draw_New_Game_Menu_text:
 DrawSmallText(36,27, New_Game_Text)
 DrawSmallText(36,34, New_Game_Text + 9)
 DrawSmallText(36,41, New_Game_Text + 19)
 DrawSmallText(36,48, New_Game_Text + 29)

 LD A, 1
 LD (Sprite_Width), A 
 
Main_Menu_Get_Key:

 LD IX, Main_Menu_Cursor
 LD A, (Player1 + Player_Screen_Y)
 LD L, A
 LD A, (Player1 + Player_Screen_X)
 LD B, 5
 call Put_Sprite_OR

 call fastCopyWithoutClear
Main_Menu_Get_CSC:

 
 LD A, (Lobby_Host_Or_Join)
 OR A
 JR NZ, Main_Menu_Get_CSC_2

 CALL bell_RecvByte
 JR NZ, Main_Menu_Get_CSC_3
 
 LD (Lobby_Host_Or_Join), A
 JR Main_Menu_Get_CSC_3

Main_Menu_Get_CSC_2:
 LD A, Other_Calculator_Ready 
 call bell_SendByte

Main_Menu_Get_CSC_3:

 B_CALL _GetCSC
 OR A
 jr z, Main_Menu_Get_CSC 
 
 cp skDown
 jr z, Main_Menu_Down
 cp skUp
 jr z, Main_Menu_Up
 cp skEnter
 jp z, Main_Menu_Options
 jr Main_Menu_Get_CSC


Main_Menu_Down:
 
 LD A, (Player1 + Player_Screen_Y)
 ADD A, 7
 CP 56
 JR Z, Check_If_Main_Menu

Check_63:
 CP 63
 CALL Z, Set_Main_Menu_Top

Check_Done:
 PUSH AF
Redraw_Menu_Sprite:

 LD D, 28
 LD B, 33
 LD A, (Player1 + Player_Screen_Y) 
 LD E, A
 ADD A, 5
 LD C, A
 LD A, RectangleFilledWhite
 LD (LineColor), a
 call _Rectangle_
 POP AF
 LD (Player1 + Player_Screen_Y), A

 jp Main_Menu_Get_Key

Check_If_Main_Menu:
 BIT Main_Menu_Or_New_Game_Menu, (IY + Flag)
 JR NZ, Check_63  ;Main Menu, so we are not at the bottom yet
 LD A, 28
 JR Check_Done

Set_Main_Menu_Top:
 LD A, 28
 RET

Set_Main_Menu_Bottom:

 BIT Main_Menu_Or_New_Game_Menu, (IY + Flag)
 JR Z, Set_49 

 LD A, 56
 RET

Set_49:
 
 LD A, 49
 RET

Main_Menu_Up:

 LD A, (Player1 + Player_Screen_Y)

 SUB 7
 CP 21
 CALL Z, Set_Main_Menu_Bottom
 PUSH AF
 jr Redraw_Menu_Sprite



Main_Menu_Options:

  BIT Main_Menu_Or_New_Game_Menu, (IY + Flag)
  JR Z, Main_Menu_Options_2

  LD A, (Player1 + Player_Screen_Y)
  CP 28
  JR Z, Prepare_For_New_Game_Menu
  CP 56
  JR Z, End_Program
  JP Main_Menu_Get_Key

Prepare_For_New_Game_Menu:

 RES Main_Menu_Or_New_Game_Menu, (IY + Flag)
 JP Draw_Menu_Background

Main_Menu_Options_2:

  BIT Main_Menu_Or_New_Game_Menu, (IY + Flag)
  JR NZ, Main_Menu_Options

  LD A, (Player1 + Player_Screen_Y)
  CP 28
  JR Z, Set_Up_Skirmish_Menu
  CP 35
  JR Z, Set_Up_Host_Menu
  CP 42
  JP Z, Set_Up_Join_Menu
  CP 49
  JR Z, Prepare_For_Main_Menu

  JP Main_Menu_Get_Key

Prepare_For_Main_Menu:

 SET Main_Menu_Or_New_Game_Menu, (IY + Flag)

 LD HL, 28*256+29
 LD (Player1 + Player_Screen_X), HL 

 JP Draw_Menu_Background


End_Program:
 bcall(_JForceCmdNoChar)


Draw_Connecting:
 BIT AI_Or_Player2, (IY + Flag)
 JP Z, Player_Settings

 Call Draw_Main_Menu_Background

 DrawSmallText(28,38,Connecting)
 call fastCopyWithoutClear
 ret




;-------------------------------
; This will take you to the
; multiplayer lobby
;-------------------------------
Set_Up_Skirmish_Menu:

 RES Player_Host_Or_Join, (IY + Flag)
 RES AI_Or_Player2, (IY + Flag)
 CALL Multiplayer_Lobby
 JP Start_Program


Set_Up_Host_Menu:
 RES Player_Host_Or_Join, (IY + Flag)
 SET AI_Or_Player2, (IY + Flag)
 
 CALL Draw_Connecting

;Does the second calculator have the hosting or joining information?

Set_Up_Host_Menu_2:

 LD A, Host
 call bell_SendByte

 LD A, (Lobby_Host_Or_Join)
 OR A
 JR NZ, Set_Up_Host_Menu_3

 call bell_RecvByte

 CP Other_Calculator_Ready
 JR NZ, Set_Up_Host_Menu_2


Set_Up_Host_Menu_3:

 CP Host
 JP Z, Error_Cannot_Host

 
 CALL Multiplayer_Lobby
 JP Start_Program



Set_Up_Join_Menu:
 SET Player_Host_Or_Join, (IY + Flag)
 SET AI_Or_Player2, (IY + Flag)
 CALL Draw_Connecting


;Does the second calculator have the hosting or joining information?

Set_Up_Join_Menu_2:
 LD A, Join
 call bell_SendByte

 LD A, (Lobby_Host_Or_Join)
 OR A
 JR NZ, Set_Up_Join_Menu_3

 call bell_RecvByte

 CP Other_Calculator_Ready
 JR NZ, Set_Up_Join_Menu_3


Set_Up_Join_Menu_3:

 CP Join
 JP Z, Error_Cannot_Join
 
 CALL Multiplayer_Lobby
 JP Start_Program


Error_Cannot_Host:
 CALL Draw_Main_Menu_Background

 DrawSmallTextVariableLength(19,37, Cannot_Host, 17)
 DrawSmallText(33,44,Cannot_Host + 17)
 call fastCopyWithoutClear

Error_Cannot_Host_Get_CSC:

 B_CALL _GetCSC
 OR A
 JR Z, Error_Cannot_Host_Get_CSC
 
 JP Draw_Menu_Background

Error_Cannot_Join:
 CALL Draw_Main_Menu_Background

 DrawSmallText(27,37,Cannot_Join)
 DrawSmallText(32,44,Cannot_Join + 14)
 call fastCopyWithoutClear
 
Error_Cannot_Join_Get_CSC:
 B_CALL _GetCSC
 OR A
 JR Z, Error_Cannot_Join_Get_CSC
 
 JP Draw_Menu_Background

Draw_Main_Menu_Background

 LD IX, Main_Menu_Box
 LD A, 12
 LD (Sprite_Width), A
 XOR A
 LD L, 23
 LD B, 41
 call Put_Sprite_Overwrite
 ret


Draw_Splash_Screen_Background:
 LD HL, 28*256+29
 LD (Player1 + Player_Screen_X), HL 

 LD IX, Splash_Screen
 LD A, 12
 LD (Sprite_Width), A
 XOR A
 LD L, 0
 LD B, 96
 call Put_Sprite_Overwrite
 
 ret

Main_Menu_Text:

 .db "New Game", 0
 .db "Load Game", 0
 .db "Campaign", 0
 .db "Credits", 0
 .db "Exit", 0

New_Game_Text:

 .db "Skirmish", 0
 .db "Host Game", 0
 .db "Join Game", 0
 .db "Back", 0

Cannot_Host:

 .db "The game already",0
 .db "has a host.",0

Cannot_Join:

 .db "You must host",0
 .db "the game.",0


#include "MainMenuGraphics.asm"